package fr.mehfile.gallerie;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import android.graphics.drawable.Drawable;

import fr.mehfile.R;
import fr.mehfile.view.utils.Utils;

public class GallerieHandler {

	private TreeMap<String, List<Integer>> albumsPhotos = new TreeMap<String, List<Integer>>();
	private TreeMap<String, Integer> albumsMiniature = new TreeMap<String, Integer>();
	private HashMap<Integer,Drawable> imageViewIdMap = new HashMap<Integer, Drawable>();
	
	private static GallerieHandler gallerieHandler;
	
	private GallerieHandler(){
		this.albumsMiniature.put("Ashre Zainabia 2012", R.drawable.gallerie_ashre_0972);
		this.albumsMiniature.put("Moharam 2011", R.drawable.gallerie_moh12_65543);
		this.albumsMiniature.put("Moharam 2012",R.drawable.gallerie_moh2_0067);
		this.albumsMiniature.put("Ramadhan 2011",R.drawable.gallerie_ram_1574);
		
		this.albumsPhotos.put("Ashre Zainabia 2012", Arrays.asList(
				R.drawable.gallerie_ashre_0484,
				R.drawable.gallerie_ashre_0490,
				R.drawable.gallerie_ashre_0972,
				R.drawable.gallerie_ashre_1016,
				R.drawable.gallerie_ashre_1057,
				R.drawable.gallerie_ashre_1082_1));
		
		this.albumsPhotos.put("Moharam 2011", Arrays.asList(
				R.drawable.gallerie_moh12_4437,
				R.drawable.gallerie_moh12_4438,
				R.drawable.gallerie_moh12_64995,
				R.drawable.gallerie_moh12_65200,
				R.drawable.gallerie_moh12_65314,
				R.drawable.gallerie_moh12_65331,
				R.drawable.gallerie_moh12_65543,
				R.drawable.gallerie_moh12_65554,
				R.drawable.gallerie_moh12_65587,
				R.drawable.gallerie_moh12_65592,
				R.drawable.gallerie_moh12_65609,
				R.drawable.gallerie_moh12_66371,
				R.drawable.gallerie_moh12_66695,
				R.drawable.gallerie_moh12_66824));
		
		this.albumsPhotos.put("Moharam 2012", Arrays.asList(
				R.drawable.gallerie_moh2_0067,
				R.drawable.gallerie_moh2_9007,
				R.drawable.gallerie_moh2_9015,
				R.drawable.gallerie_moh2_9772,
				R.drawable.gallerie_moh2_9777,
				R.drawable.gallerie_moh2_9786));
		
		this.albumsPhotos.put("Ramadhan 2011", Arrays.asList(
				R.drawable.gallerie_ram_1506,
				R.drawable.gallerie_ram_1509,
				R.drawable.gallerie_ram_1518,
				R.drawable.gallerie_ram_1522,
				R.drawable.gallerie_ram_1524,
				R.drawable.gallerie_ram_1543,
				R.drawable.gallerie_ram_1574,
				R.drawable.gallerie_ram_1878,
				R.drawable.gallerie_ram_1880,
				R.drawable.gallerie_ram_1912,
				R.drawable.gallerie_ram_1934,
				R.drawable.gallerie_ram_1969,
				R.drawable.gallerie_ram_2022,
				R.drawable.gallerie_ram_2135,
				R.drawable.gallerie_ram_2189));
		
	}
	
	public static GallerieHandler getInstance(){
		if(gallerieHandler == null){
			gallerieHandler = new GallerieHandler();
		}
		
		return gallerieHandler;
	}

	public int getNumberOfAlbums() {
		return albumsPhotos.size();
	}

	public String getAlbumNameByIndex(int i) {
		List<String> listAlbumNames = new ArrayList(this.albumsPhotos.keySet());
		return listAlbumNames.get(i);
	}

	public int getAlbumSizeByIndex(int i) {
		String albumName = this.getAlbumNameByIndex(i);
		return albumsPhotos.get(albumName).size();
	}

	public int getAlbumSizeByName(String albumName) {
		return albumsPhotos.get(albumName).size();
	}
	
	public int getImageId(String albumName, int position){
		return albumsPhotos.get(albumName).get(position);
	}
	
	public int getVignette(String albumName){
		return albumsMiniature.get(albumName);
		
	}

	public void saveMapImageViewIdMap(int id, Drawable drawable) {
		imageViewIdMap.put(id, drawable);
	}

	public Drawable getDrawableFromImageViewId(int previousImageView) {
		return this.imageViewIdMap.get(previousImageView);
	}

	public int getPreviousImageId(String albumName, int currentImageView) {
		List<Integer> images = this.albumsPhotos.get(albumName);
		int indexImage = images.indexOf(currentImageView);
		if (indexImage != -1){
		  	if (indexImage == 0){
		   		return images.get(images.size()-1);    		
		    }
		    else{
		    	return images.get(indexImage-1);
		    }
		}
		return 0;
	}

	public int getNextImageId(String albumName, int currentImageView) {
		List<Integer> images = this.albumsPhotos.get(albumName);
		int indexImage = images.indexOf(currentImageView);
		if (indexImage != -1){
		   if (indexImage == images.size() -1){
			   return images.get(0);
		   }
		   else{
		    	return images.get(indexImage+1);
		   }
		}
		return 0;
	}
}
